<?php
/**
 * Created by PhpStorm.
 * User: hgzyc
 * Date: 18-4-21
 * Time: 下午12:50
 */

namespace App\Http\Controllers;


use App\Http\Helper\CrontabFile;
use App\Model\CronModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Cron
{
    public function cron_list(Request $request){
        $all= CronModel::all();

       return view('cron.list')->with('all',$all);

    }
    public function cron_add(Request $request)
    {
        return view('cron.add');
    }
    public function cron_change(Request $request){
        $id=$request->input('id',0);
        if((int)$id ==0){
            return redirect('/list');
        }
        $model=CronModel::where('id',$id)->first();
        return view('cron.change')->with('model',$model);

    }
    public function cron_do_add_update(Request $request){
        $id=$request->input('id',0);
        $time=$request->input('time');
        $shell=$request->input('shell');
        if ($id==0){
        $model=new CronModel();
        }else{
            $model=CronModel::where('id',$id)->first();
        }
        $model->time=$time;
        $model->shell = $shell;
        $model->save();
    }
    public function cron_delete(Request $request){
        $id=$request->input('id',0);
        if (!$id || ! CronModel::where('id',$id)->first()){
            return ['code'=>500,'msg'=>'id无效'];
        }
        $flag=CronModel::find($id);
        if($flag->delete()){
            return ['code'=>200,'msg'=>'删除成功'];
        }else{
            return ['code'=>500,'msg'=>"删除失败"];
        }
        
    }
    public function gif(Request $request){
        Log::error(json_encode($request->all()));
    }
}